<?php
class File {
	public static function rm_ext($st){
		if (strrpos($st,'.') != 0 and strrpos($st,'.') !=false){	
			$st = substr($st,0,strlen($st)-strlen(strrchr($st,'.')));
		}
		return $st;
	}
	
	public static function get_ext($st){
		if (strrpos($st,'.') != 0  and strrpos($st,'.') !=false){
			$st = ".".substr(strrchr($st,'.'),1,strlen($st));
		}else{
			$st = "";
		}
		return $st;
	}
	
	public static function get_name_with_fullpath($st){
		$st = substr($st,1+strlen($st)-strlen(strrchr($st,'/')));
		return $st;	
	}
	
	public static function modify_parent_path($st){
		$st = substr($st,0,strlen($st)-strlen(strrchr($st,'/')));
		$st = strrchr($st,'/');
		$st = substr($st,1,strlen($st));
		return $st;
	}
	
	public static function size_modif($size){
		if($size=='2147483647'){
			return '>2Go';
		}
		elseif( $size >'1073741824') {
			$sizegb = (($size / 1024)/1024)/1024;
			$sizegb = round($sizegb,2);
			return $sizegb."Go";
		}elseif($size >'1048576') {
			$sizemb = ($size / 1024)/1024;
			$sizemb = round($sizemb,2);
			return $sizemb."Mo";
		} elseif($size > '1024') {
			$sizekb = $size / 1024;
			$sizekb = round($sizekb,2);
			return $sizekb."Ko";
		}else{
			return "$size o";
		}
	}
	
	public static function file_in_array($server){
			$sql_query=mysql_query("Select name,address,pin from ".DATABASE_TABLE_SERVER." WHERE name like \"".$server."\" ORDER BY `id` ASC") or die(mysql_error());
			$j=0;
			while ($query = mysql_fetch_array($sql_query)){
				if (Server::check_serv_up($query['address']) != "-1"){	
					$f=@fopen($query['address']."?serv_request=".md5($query['pin']),'r');
					if ($f != False){
						$e="";
						$i=0;
						while (strpos($e,"<version>") == "" and strpos($e,"<filesearch>")=="" and $i<10){
							$e=fgets($f);
							$i++;
						}
						$e=trim($e);
						$version = substr($e,(strpos($e,"<version>")+9),(strpos($e,"</version>")-9));
						$arrayser = substr($e,(strpos($e,"<filesearch>")+12),(strpos($e,"</filesearch>")-36));
						$array_file=unserialize($arrayser);
						if (! $array_file){
							$user->message->addError("ERREUR Sur client ".$query['name']);
							
						}
						else{
							if($version=="0.0.1"){
								for ($i=0;$i<count($array_file);$i++){
									$all_array_file[$j][0]=mysql_escape_string($array_file[$i][0]);
									$all_array_file[$j][1]=mysql_escape_string($array_file[$i][1]);
									$all_array_file[$j][2]=mysql_escape_string($array_file[$i][2]);
									$all_array_file[$j][3]=mysql_escape_string($array_file[$i][3]);
									$all_array_file[$j][3]="Null";
									$j++;
								}	
							}
							elseif($version=="0.2.0"){
							for ($i=0;$i<count($array_file);$i++){
									$all_array_file[$j][0]=mysql_escape_string($array_file[$i][0]);
									$all_array_file[$j][1]=mysql_escape_string($array_file[$i][1]);
									$all_array_file[$j][2]=mysql_escape_string($array_file[$i][2]);
									$all_array_file[$j][3]=mysql_escape_string($array_file[$i][3]);
									$all_array_file[$j][4]=mysql_escape_string($array_file[$i][4]);
											
									$j++;
								}
							}
							else{
								//print"client invalide";exit();
							}
						}
					}
				}	
			}
			if (count($all_array_file)==0){
				return false;
			}
			else{
				return $all_array_file;
			}
	}
	
	public static function ajax_get_last_file($sqltype,$size){
		?>
		<table id="table_last_film" >
		<caption>Last <select name="acceuil_type"  onchange="ajax_refresh_last_type(this.value,5)">
	
		<?php
			//include 'config.php';
			global $typefiles,$def_film;
			$def_array	=  $def_film;
			if($sqltype == "alltype"){
				$sqltype = '%';
			}else{
				$sqltype=mysql_escape_string($sqltype);
			}
			
			if ($sqltype == 'Other'){
				$sql_query=mysql_query("SELECT ".DATABASE_TABLE_FILE.".id,".DATABASE_TABLE_FILE.".fullpath,".DATABASE_TABLE_FILE.".type,".DATABASE_TABLE_FILE.".name, ".DATABASE_TABLE_FILE.".taille, ".DATABASE_TABLE_FILE.".date, ".DATABASE_TABLE_FILE.".server 
						FROM ".DATABASE_TABLE_FILE." WHERE  type like '$sqltype'  ORDER BY date DESC LIMIT $size");
				
			}else{
				$sql_query=mysql_query("SELECT ".DATABASE_TABLE_FILE.".id,".DATABASE_TABLE_FILE.".fullpath,".DATABASE_TABLE_FILE.".type,".DATABASE_TABLE_FILE.".name, ".DATABASE_TABLE_FILE.".taille, ".DATABASE_TABLE_FILE.".date, ".DATABASE_TABLE_FILE.".server 
						FROM ".DATABASE_TABLE_FILE." WHERE (".Sql::array_in_where_clause(DATABASE_TABLE_FILE.".name",$def_array).") and type like '$sqltype'  ORDER BY date DESC LIMIT $size");
				
			}
		?>
	
			<option value="alltype" <?php if("%"==$sqltype){print "selected=\"selected\"";} ?>>All Type</option>
		<?php
			foreach ($typefiles as $element ){
		?>
			<option value="<?php print $element ?>"  <?php if($element==$sqltype){print "selected=\"selected\"";} ?> ><?php print $element ?></option>
		<?php } ?>
		</select></caption>
		<tr>
		<td class="title" >Title</td>
		<td class="title" >Server</td>
		<td class="title" >Type</td>
		</tr>
		<?php
		while ($query = mysql_fetch_array($sql_query)){
		?>
		<tr>
			<td class="column_margin2">
				<?php 
				$arrayfav = Fav::fav_in_array($user->login);
				if(! in_array($query['fullpath'],$arrayfav)){?>
				<img alt="add" title="add in your favorite list" src="./img/add_favorite.png" onclick="ajax_add_favorite('<?php print $query['id'] ?>');Effect.Puff(this);" />
				<?php }else{?>
				<img alt="rm" title="remove your favorite file" src="./img/rm_favorite.png" onclick="ajax_rm_favorite('<?php print $query['id'] ?>');Effect.Puff(this);" />
				<?php }?>
				<a target="_blank" href="http://www.allocine.fr/recherche/?motcle=<?php print File::rm_ext($query['name']); ?>&x=0&y=0&rub=0" >
				<img style="border:0;" src="img/allo.ico" alt="BA"   /></a>
				<a href="index.php?film=<?php print md5($query['fullpath']); ?>&idserver=<?php print $query['server']; ?>">
					<?php print htmlspecialchars(stripslashes(File::rm_ext($query['name'])));?>
				</a>
			</td>
			<td><?php print $query['server']; ?></td>
			<td><?php print $query['type']; ?></td>
		</tr>
		<?php 
		}
		?>
		</table>
		<?php
	}
	
}
?>